package com.sxzbxc.weixin.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.sxzbxc.weixin.entity.DicCompanies;
import com.sxzbxc.weixin.entity.DicMelfunction;
import com.sxzbxc.weixin.entity.SysUser;
import com.sxzbxc.weixin.entity.infoMelfunction;
import com.sxzbxc.weixin.pojo.AjaxJson;
import com.sxzbxc.weixin.service.DicCompaniseServiceI;
import com.sxzbxc.weixin.service.DicMelfunctionServiceI;
import com.sxzbxc.weixin.service.InfoMelfunctionServiceI;
import com.sxzbxc.weixin.service.SysUserServiceI;

/**
 * 报表
 * @author Tom
 *
 */
@Controller
@RequestMapping("statementController")
public class StatementController {

	@Autowired
	private InfoMelfunctionServiceI infoMelfunctionService;
	
	@Autowired
	private DicMelfunctionServiceI dicMelfunctionService;
	
	@Autowired
	private DicCompaniseServiceI dicCompaniseService;
	
	@Autowired
	private SysUserServiceI sysUserService;
	
	SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm");//如2016-08-10 20:40  
	
	
	@RequestMapping(params = "toStatement")
	public ModelAndView toStatement(){
		
		return new ModelAndView("statement/jggym");
	}
	@RequestMapping(params = "toStatement1")
	public ModelAndView toStatement1(){
		
		return new ModelAndView("statement/selectwcqk");
	}
	@RequestMapping(params = "toStatement2")
	public ModelAndView toStatement2(){
		
		return new ModelAndView("statement/statementList2");
	}
	@RequestMapping(params = "toStatement3")
	public ModelAndView toStatement3(){
		
		return new ModelAndView("statement/statementList3");
	}
	@RequestMapping(params = "toStatement4")
	public ModelAndView toStatement4(){
		
		return new ModelAndView("statement/statementList4");
	}
	
	/**
	 * 人员报表选择
	 * @return
	 */
	@RequestMapping(params = "toMyTask")
	public ModelAndView toMyTask(){
		return new ModelAndView("statement/selectByPeople");
	}
	
	/**
	 * 用时情况统计
	 * @return
	 */
	@RequestMapping(params = "toDTask")
	public ModelAndView toDTask(){
		return new ModelAndView("statement/selectysqk");
	}
	
	/**
	 * 当日工单完成情况
	 * @param request
	 * @param id
	 * @return
	 */
	@RequestMapping(params = "wdatagrid")
	@ResponseBody
	public AjaxJson wdatagrid(){
		AjaxJson j = new AjaxJson();
		Integer id=0;
		List<infoMelfunction> selectByDate = infoMelfunctionService.selectByDate(id);
		Map<String , Integer> w_status = new HashMap<String , Integer> (); 
		Map<String , Map<String , Integer>> map = new HashMap<String , Map<String ,Integer>>();
		for (infoMelfunction infoMelfunction : selectByDate) {
			Integer infoStatus = infoMelfunction.getInfoStatus();
			w_status.put("off", 0);
			w_status.put("on",0);
			if(infoStatus==0){//未完成
				String ids = infoMelfunction.getInfoForDepart();//获得人员
				DicCompanies depart = dicCompaniseService.selectByPrimaryKey(ids);
				String name = depart.getComPlaceName();
				if(map.containsKey(name)){
					w_status = map.get(name);
					if(w_status.containsKey("off")){
						Integer k = w_status.get("off");
						w_status.put("off", k+1);
					}else{
						w_status.put("off",1);
					}
				}else{
					w_status.put("off", 1);
					map.put(name, w_status);
				}
			}else if(infoStatus==1){//完成
				String ids = infoMelfunction.getInfoForDepart();//获得人员
				DicCompanies depart = dicCompaniseService.selectByPrimaryKey(ids);
				String name = depart.getComPlaceName();
				if(map.containsKey(name)){//已有该人员的数据
					w_status = map.get(name);
					if(w_status.containsKey("on")){
						Integer k = w_status.get("on");
						w_status.put("on", k+1);
					}else{
						w_status.put("on",1);
					}
					map.put(name, w_status);
				}else{
					w_status.put("on", 1);
					map.put(name, w_status);
				}
			}
		}
		j.setObj(map);
        j.setSuccess(true);
		return j;
	}
	/**
	 * 查看人员绩效报表
	 * @param request
	 * @param id
	 * @return
	 */
	@RequestMapping(params = "pdatagrid")
	@ResponseBody
	public AjaxJson datagrid(HttpServletRequest request , Integer id){
		AjaxJson j = new AjaxJson();
		List<infoMelfunction> selectByDate = infoMelfunctionService.selectByDate(id);
		Map<String , Integer> map = new HashMap<String , Integer>();
		for (infoMelfunction infoMelfunction : selectByDate) {
			String name = infoMelfunction.getInfoUpdateName();
			if(map.containsKey(name)){
				Integer i = map.get(name)+1;
				map.put(name, i);
			}else{
				map.put(name, 1);
			}
		}
		j.setObj(map);
        j.setSuccess(true);
		return j ;
	}
	
	
	/**
	 * 故障报表选择
	 * @return
	 */
	@RequestMapping(params = "toTypeTaskNum")
	public ModelAndView toTypeTaskNum(){
		return new ModelAndView("statement/selectByInmelInfo");
	}
	
	@RequestMapping(params = "mdatagrid")
	@ResponseBody
	public AjaxJson mdatagrid(HttpServletRequest request , Integer id){
		AjaxJson j = new AjaxJson();
		List<infoMelfunction> selectByDate = infoMelfunctionService.selectByDate(id);
		Map<String , Integer> map = new HashMap<String , Integer>();
		for (infoMelfunction infoMelfunction : selectByDate) {
			String melinfo = infoMelfunction.getInfoMelfunctionType();
			DicMelfunction selectByMelCode = dicMelfunctionService.selectByPrimaryKey(melinfo);
			String dicMelname = selectByMelCode.getDicMelname();
			if(map.containsKey(dicMelname)){
				Integer i = map.get(dicMelname);
				map.put(dicMelname, i+1);
			}else{
				map.put(dicMelname, 1);
			}
		}
		j.setObj(map);
        j.setSuccess(true);
		return j ;
	}
	
	/**
	 * 工单用时完成
	 * @param request
	 * @param id
	 * @return
	 */
	@RequestMapping(params = "ddatagrid")
	@ResponseBody
	public AjaxJson ddatagrid(HttpServletRequest request, Integer id){
		AjaxJson j = new AjaxJson();
		Map<String , Double> avg = new HashMap<String ,Double> ();
		List<infoMelfunction> selectByDate = infoMelfunctionService.selectByDate(id);
		Map<String , Integer> map = new HashMap<String , Integer>();
		for (infoMelfunction infoMelfunction : selectByDate) {
			String infoForDepart = infoMelfunction.getInfoForDepart();//供电所ID
			DicCompanies depart = dicCompaniseService.selectByPrimaryKey(infoForDepart);//相关供电所数据
			String comPlaceName = depart.getComPlaceName();//具体供电所名字
			Date createDate = infoMelfunction.getInfoCreateDate();
			Date updateDate = infoMelfunction.getInfoUpdateDate();
			int avgForMinute = avgForMinute(createDate ,updateDate);
			if(map.containsKey(comPlaceName)){
				Integer i = map.get(comPlaceName);
				map.put(comPlaceName, i+avgForMinute);
			}else{
				map.put(comPlaceName, avgForMinute);
			}
		}
		for (Entry<String, Integer> entry : map.entrySet()	) { 
			  String key = entry.getKey();
			  Integer value = entry.getValue();
			  int number = number(id , key);
			  Double avgTime =(double) value/number;
			  avg.put(key, avgTime);
		}
		j.setObj(avg);
		j.setSuccess(true);
		return j;
	}
	
	
	/**
	 * 算取某工单用时
	 * @param create
	 * @param update
	 * @return
	 */
	public int avgForMinute(Date create , Date update){
		String createDate = simpleFormat.format(create);
		String updateDate = simpleFormat.format(update);
		int minutes = 0;
		try {
			long from = simpleFormat.parse(createDate).getTime();  
			long to = simpleFormat.parse(updateDate).getTime();
			minutes = (int) ((to - from)/(1000 * 60));  
		} catch (ParseException e) {
			e.printStackTrace();
		}  
		return minutes;
	}
	
	
	/**
	 * 获取规定时间内某供电所的数据数目
	 * @param id //时间段编号
	 * @param departId 供电站中文
	 * @return
	 */
	public int number(Integer id , String departId){
		List<infoMelfunction> selectByDate = infoMelfunctionService.selectByDate(id);
		int i = 0;
		for (infoMelfunction infoMelfunction : selectByDate) {
			String infoForDepart = infoMelfunction.getInfoForDepart();//供电站ID
			DicCompanies companies = dicCompaniseService.selectByPrimaryKey(infoForDepart);
			String departName = companies.getComPlaceName();//供电所中文
			if(departName.equals(departId)){
				i=i+1;
			}
		}
		return i;
	}
}
